home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / GAMESDS2.DMS / in.adf / GDS_Tutorial.lha / Examples / picture / ar_viewer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-15  |  17.3 KB  |  537 lines

  1. /*******************************************************************************************
  2.  
  3. This quick & dirty picture viewer was put together for Amiga Report online magazine, a not-
  4. for-profit organization.  It makes use of the GameSmith ILBM picture loader.  For more
  5. information about the GameSmith Development System, contact:
  6.  
  7.    Bithead Technologies
  8.    8085 North Raleigh Place
  9.    Westminster, Colorado 80030-4316
  10.    USA
  11.    (303) 427-9521
  12.  
  13. This source code is hereby placed in the public domain.
  14.  
  15. USAGE: ar_viewer (filename) [X offset] [Y offset] [Workbench]
  16.  
  17. *******************************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #include <exec/types.h>
  23. #include <intuition/intuition.h>
  24. #include <intuition/intuitionbase.h>
  25. #include <graphics/gfx.h>
  26. #include <clib/exec_protos.h>
  27. #include <clib/intuition_protos.h>
  28. #include <clib/graphics_protos.h>
  29. #include <pragmas/exec_sysbase_pragmas.h>
  30. #include <pragmas/intuition_pragmas.h>
  31. #include <pragmas/graphics_pragmas.h>
  32.  
  33. #include "GameSmith:include/libraries/libptrs.h"
  34. #include "GameSmith:GameSmith.h"
  35.  
  36. #define FONT_WIDTH   8
  37. #define FONT_HEIGHT   8
  38.  
  39. #define CUSTOM_FLAGS   (BORDERLESS|SUPER_BITMAP|ACTIVATE|NOCAREREFRESH|GIMMEZEROZERO|WINDOWCLOSE)
  40. #define WB_FLAGS      (SUPER_BITMAP|ACTIVATE|WINDOWSIZING|WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|GIMMEZEROZERO|NOCAREREFRESH)
  41.  
  42. #define CUSTOM_SCREEN   0
  43. #define WB_SCREEN         1
  44.  
  45. /* ----------------------------------------------------------------------- */
  46. /* Function prototypes:                                                    */
  47.  
  48. int get_wb_info(void);
  49. int load_image(char *);
  50. int display_image(char *,int);
  51. int display_image_window(char *);
  52. int display_image_screen(char *);
  53.  
  54. /* ----------------------------------------------------------------------- */
  55.  
  56. int left,right,top,bottom,barheight,width,height,depth;
  57. unsigned long color[256];         /* color table */
  58.  
  59. char screen_title[]="GameSmith® IFF ILBM Loader";
  60.  
  61. char topaz8_text[]="topaz.font";
  62.  
  63. struct TextAttr topaz8 =
  64.    {
  65.    topaz8_text,                  /* Addr of ASCIIZ string containing font name */
  66.    8,                              /* font YSize (height) */
  67.    FS_NORMAL,                     /* style flags */
  68.    FPF_DESIGNED|FPF_ROMFONT      /* flags */
  69.    };
  70.  
  71. unsigned short pens[] = {0xffff};   /* needed for new (3d) look */
  72.  
  73. struct TagItem tags[] = {
  74.    {
  75.    SA_Pens,
  76.    (unsigned long)pens
  77.    },
  78.    {
  79.    SA_Width,
  80.    STDSCREENWIDTH
  81.    },
  82.    {
  83.    SA_Height,
  84.    STDSCREENHEIGHT
  85.    },
  86.    {
  87.    SA_Depth,
  88.    0L
  89.    },
  90.    {
  91.    SA_AutoScroll,
  92.    1L
  93.    },
  94.    {
  95.    SA_Type,
  96.    CUSTOMSCREEN
  97.    },
  98.    {
  99.    SA_Quiet,
  100.    1L
  101.    },
  102.    {
  103.    SA_Overscan,
  104.    OSCAN_TEXT
  105.    },
  106.    {
  107.    TAG_DONE,
  108.    0L
  109.    }
  110.    };
  111.  
  112. struct ExtNewScreen newscreen =
  113.    {
  114.    0,0,                           /* left & top edges */
  115.    0,0,0,                        /* width, height, & depth (filled) */
  116.    1,2,                           /* detail & block pens */
  117.    0,                              /* view modes (filled) */
  118.    CUSTOMSCREEN|SCREENQUIET|AUTOSCROLL|
  119.    NS_EXTENDED,                  /* type */
  120.    &topaz8,                        /* text attribute for font */
  121.    NULL,                           /* screen title */
  122.    NULL,                           /* ptr to gadgets */
  123.    NULL,                           /* ptr to bitmap */
  124.    tags                           /* addr of screen tags for V6 and later */
  125.    };
  126.  
  127. char custom_text[]="Custom Screen";
  128. char wb_text[]="WorkBench Window";
  129. char quit_text[]="Quit";
  130.  
  131. struct IntuiText item1b_it =
  132.    {
  133.    1,0,                        /* Front & Back pens (color reg #) */
  134.    JAM1,                        /* Draw mode */
  135.    0,1,                        /* left & top edge offsets */
  136.    &topaz8,                     /* ptr to font desc. (ROM font) */
  137.    quit_text,                  /* addr of actual text string */
  138.    NULL                        /* ptr to next intuitext struct */
  139.    };
  140.  
  141. struct MenuItem item1b = 
  142.    {
  143.    NULL,                        /* ptr to next menu item */
  144.    0,                           /* left edge */
  145.    FONT_HEIGHT+2,               /* top edge */
  146.    17*FONT_WIDTH,               /* width (# chars * width of font) */
  147.    FONT_HEIGHT+2,               /* height */
  148.    ITEMTEXT|
  149.    ITEMENABLED|HIGHCOMP,      /* flags */
  150.    0,                           /* mutual exclusion indicator (none) */
  151.    (APTR)&item1b_it,            /* ptr to item fill */
  152.    NULL,                        /* select fill (don't change, just highlight) */
  153.    0,                           /* command, or hotkey */
  154.    NULL,                        /* ptr to 1st subitem */
  155.    NULL                        /* ptr to next selected item (system filled) */
  156.    };
  157.  
  158. struct IntuiText item1a_it =
  159.    {
  160.    1,0,                        /* Front & Back pens (color reg #) */
  161.    JAM1,                        /* Draw mode */
  162.    0,1,                        /* left & top edge offsets */
  163.    &topaz8,                     /* ptr to font desc. (ROM font) */
  164.    custom_text,               /* addr of actual text string */
  165.    NULL                        /* ptr to next intuitext struct */
  166.    };
  167.  
  168. struct MenuItem item1a = 
  169.    {
  170.    &item1b,                     /* ptr to next menu item */
  171.    0,                           /* left edge */
  172.    0,                           /* top edge */
  173.    17*FONT_WIDTH,               /* width (# chars * width of font) */
  174.    FONT_HEIGHT+2,               /* height */
  175.    ITEMTEXT|
  176.    ITEMENABLED|HIGHCOMP,      /* flags */
  177.    0,                           /* mutual exclusion indicator (none) */
  178.    (APTR)&item1a_it,            /* ptr to item fill */
  179.    NULL,                        /* select fill (don't change, just highlight) */
  180.    0,                           /* command, or hotkey */
  181.    NULL,                        /* ptr to 1st subitem */
  182.    NULL                        /* ptr to next selected item (system filled) */
  183.    };
  184.  
  185. char menu1_text[]="Display";
  186.  
  187. struct Menu menu1 =
  188.    {
  189.    NULL,                  /* ptr to next menu struct */
  190.    0,0,                  /* left edge, top edge */
  191.    8*FONT_WIDTH,         /* width ((# chars + 1) * width of font) */
  192.    FONT_HEIGHT,         /* height (ignored by intuition) */
  193.    MENUENABLED,         /* flags */
  194.    menu1_text,            /* ptr to ASCIIZ name string */
  195.    &item1a               /* ptr to 1st menu item */
  196.    };
  197.  
  198. BitMapHeader bmh;
  199.  
  200. struct loadILBM_struct loadimg = 
  201.    {
  202.    NULL,               /* ptr to picture name string */
  203.    NULL,               /* ptr to 1st bitmap */
  204.    NULL,               /* ptr to 2nd bitmap (if any) */
  205.    color,            /* ptr to color table array */
  206.    256,               /* # colors in color table */
  207.    NULL,               /* height of image in pixels (filled by load call) */
  208.    NULL,               /* width of image in pixels (filled) */
  209.    NULL,               /* x display offset (filled) */
  210.    NULL,               /* y display offset (filled) */
  211.    NULL,               /* pic mode (filled) */
  212.    0,                  /* x load offset (from left) in bytes */
  213.    0,                  /* y load offset (from top) in rows */
  214.    ILBM_ALLOC1|ILBM_COLOR,   /* flags (alloc 1 bitmap, fill color table) */
  215.    0xff,               /* bitplane fill mask */
  216.    0xff,               /* bitplane load mask */
  217.    &bmh               /* address of BitMapHeader to fill (optional) */
  218.    };
  219.  
  220. struct NewWindow pic_win=
  221.    {
  222.    0,0,0,0,            /* left edge, top edge, width, height */
  223.    1,2,               /* detail & block pens */
  224.    CLOSEWINDOW|MENUPICK,   /* IDCMP flags */
  225.    WB_FLAGS,         /* window flags */
  226.    NULL,               /* ptr to 1st gadget */
  227.    NULL,               /* ptr to checkmark image */
  228.    NULL,               /* window title */
  229.    NULL,               /* ptr to screen */
  230.    NULL,               /* ptr to bitmap to use for refresh */
  231.    30,30,0,0,         /* min width & height, max width & height */
  232.    WBENCHSCREEN      /* window type */
  233.    };
  234.  
  235. struct NewWindow pic_win2=
  236.    {
  237.    0,0,0,0,            /* left edge, top edge, width, height */
  238.    1,2,               /* detail & block pens */
  239.    CLOSEWINDOW|MENUPICK,   /* IDCMP flags */
  240.    CUSTOM_FLAGS,      /* window flags */
  241.    NULL,               /* ptr to 1st gadget */
  242.    NULL,               /* ptr to checkmark image */
  243.    NULL,               /* window title */
  244.    NULL,               /* ptr to screen */
  245.    NULL,               /* ptr to bitmap to use for refresh */
  246.    0,0,0,0,            /* min width & height, max width & height */
  247.    CUSTOMSCREEN      /* window type */
  248.    };
  249.  
  250. /***************************************************************************/
  251.  
  252. main(argc,argv)
  253. int argc;
  254. char *argv[];
  255.  
  256. {
  257.    int err;
  258.  
  259.    if (gs_open_libs(INTUITION|DOS|GRAPHICS,0))
  260.       exit(-99);                        /* open necessary Amiga libs */
  261.    if (argc < 2)                        /* check # command line arguements */
  262.       {
  263.       DisplayBeep(NULL);               /* if error, flash the screen */
  264.       gs_close_libs();                  /* close Amiga libs */
  265.       exit(-98);                        /* 1st arg has to be name of file to display */
  266.       }
  267.    if (err=get_wb_info())               /* get workbench size, depth, & window border info */
  268.       {
  269.       DisplayBeep(NULL);               /* if error, flash the screen */
  270.       gs_close_libs();                  /* close Amiga libs */
  271.       exit(err);
  272.       }
  273.    if (argc >= 3)
  274.       pic_win.LeftEdge=atoi(argv[2]);   /* set left edge of window */
  275.    if (argc >= 4)
  276.       pic_win.TopEdge=atoi(argv[3]);   /* set top edge of window */
  277.    if (err=load_image(argv[1]))         /* load image into it's own bitmap */
  278.       {
  279.       DisplayBeep(NULL);               /* if error, flash the screen */
  280.       gs_close_libs();                  /* close Amiga libs */
  281.       exit(err);
  282.       }
  283. /*   printf("\n%d x %d x %d\n",bmh.w,bmh.h,bmh.nPlanes); */   /* print dimensions */
  284.    if (argc >= 5)                        /* if 5th arguement */
  285.       err=display_image(argv[1],WB_SCREEN);   /* show on WB screen */
  286.    else
  287.       err=display_image(argv[1],CUSTOM_SCREEN);   /* else show on custom screen */
  288.    if (err)                              /* if display error */
  289.       {
  290.       DisplayBeep(NULL);               /* flash the screen */
  291.       gs_free_bitmap(loadimg.bitmap1);   /* free up bitmap we alloced by image load */
  292.       gs_close_libs();                  /* close Amiga libs */
  293.       exit(err);
  294.       }
  295.    gs_free_bitmap(loadimg.bitmap1);      /* free up bitmap we alloced by image load */
  296.    gs_close_libs();                     /* close Amiga libs */
  297.    exit(err);
  298. }
  299.  
  300. /***************************************************************************/
  301.  
  302. int get_wb_info()
  303.  
  304. {
  305.    struct Screen *s,screen13;      /* for polling WB attributes */
  306.  
  307.    if (IntuitionBase->LibNode.lib_Version >= 36)   /* if OS 2.0 or above */
  308.       {                           /* get info about workbench screen */
  309.       if (!(s=LockPubScreen("Workbench")))
  310.          return(-97);
  311.       left=s->WBorLeft;
  312.       right=s->WBorRight;
  313.       top=s->WBorTop;
  314.       bottom=s->WBorBottom;
  315.       barheight=s->BarHeight;
  316.       width=s->Width;
  317.       height=s->Height;
  318.       depth=s->BitMap.Depth;
  319.       UnlockPubScreen(NULL,s);
  320.       }
  321.    else      /* else use old 1.3 method of polling WB attributes */
  322.       {
  323.       if (!GetScreenData(&screen13,sizeof(struct Screen),WBENCHSCREEN,NULL))
  324.          return(-97);
  325.       left=screen13.WBorLeft;
  326.       right=screen13.WBorRight;
  327.       top=screen13.WBorTop;
  328.       bottom=screen13.WBorBottom;
  329.       barheight=screen13.BarHeight;
  330.       width=screen13.Width;
  331.       height=screen13.Height;
  332.       depth=screen13.BitMap.Depth;
  333.       }
  334.    return(0);
  335. }
  336.  
  337. /***************************************************************************/
  338.  
  339. int load_image(file)
  340. char *file;
  341.  
  342. {
  343.    loadimg.file=file;
  344.    return(gs_loadILBM(&loadimg));      /* load the ILBM file */
  345. }
  346.  
  347. /***************************************************************************/
  348.  
  349. int display_image(file,target)
  350. char *file;
  351. int target;
  352.  
  353. {
  354.    int retcode=1;
  355.    
  356.    if (target == WB_SCREEN)
  357.       {
  358.       while (retcode > 0)
  359.          {
  360.          retcode=display_image_window(file);
  361.          if (retcode > 0)
  362.             retcode=display_image_screen(file);
  363.          }
  364.       }
  365.    else
  366.       {
  367.       while (retcode > 0)
  368.          {
  369.          retcode=display_image_screen(file);
  370.          if (retcode > 0)
  371.             retcode=display_image_window(file);
  372.          }
  373.       }
  374.    return(retcode);
  375. }
  376.  
  377. /***************************************************************************/
  378.  
  379. int display_image_window(file)
  380. char *file;
  381.  
  382. {
  383.    int done=0,retcode=0,right_edge,bottom_edge;
  384.    struct IntuiMessage *msg;
  385.    struct Window *win;
  386.    struct ExecBase *SysBase=*(struct ExecBase **)4;
  387.  
  388.                                        /* now set window width & height */
  389.    pic_win.Width=bmh.w+left+right+14;
  390.    pic_win.Height=bmh.h+top+bottom+9;
  391.    pic_win.MaxWidth=pic_win.Width;      /* set maximum window width & height */
  392.    pic_win.MaxHeight=pic_win.Height;
  393.    pic_win.TopEdge+=barheight+1;
  394.    pic_win.BitMap=loadimg.bitmap1;      /* ptr to refresh bitmap */
  395.    if ((pic_win.Width+pic_win.LeftEdge) > width) /* make sure window fits in WB */
  396.       pic_win.Width=width-pic_win.LeftEdge;
  397.    if ((pic_win.Height+pic_win.TopEdge) > height)
  398.       pic_win.Height=height-pic_win.TopEdge;
  399.    pic_win.Title=file;                  /* title is name of file */
  400.    if (!(win=OpenWindow(&pic_win)))
  401.       return(-96);                     /* unable to open window */
  402.    if ((win->BorderRight != 18) || (win->BorderTop != 11) ||
  403.       (win->BorderLeft != 4) || (win->BorderBottom != 2))
  404.       {                                 /* make sure we got window borders correct */
  405.       right_edge=bmh.w+win->BorderLeft+win->BorderRight;
  406.       bottom_edge=bmh.h+win->BorderTop+win->BorderBottom;
  407.       WindowLimits(win,30,30,right_edge,bottom_edge);
  408.       SizeWindow(win,right_edge-win->Width,bottom_edge-win->Height);
  409.       }
  410.    SetWindowTitles(win,(UBYTE *)-1,screen_title);
  411.    item1a_it.IText=custom_text;
  412.    SetMenuStrip(win,&menu1);            /* tack on the menu */
  413.    while (!done)
  414.       {
  415.       WaitPort(win->UserPort);         /* wait for Intuition message */
  416.       msg=(struct IntuiMessage *)GetMsg(win->UserPort);
  417.       switch (msg->Class)
  418.          {
  419.          case CLOSEWINDOW:
  420.             done=1;
  421.             break;
  422.          case MENUPICK:
  423.             switch (ITEMNUM(msg->Code))
  424.                {
  425.                case 0:
  426.                   retcode=1;
  427.                case 1:
  428.                   done=1;
  429.                   break;
  430.                default:
  431.                   break;
  432.                }
  433.             break;
  434.          default:
  435.             break;
  436.          }
  437.       ReplyMsg((struct Message *)msg);   /* tell Intuition we're done with msg, thanx */
  438.       }
  439.    SetWindowTitles(win,(UBYTE *)-1,NULL);
  440.    ClearMenuStrip(win);                  /* remove menu strip */
  441.    CloseWindow(win);
  442.    pic_win.TopEdge-=barheight+1;
  443.    return(retcode);
  444. }
  445.  
  446. /***************************************************************************/
  447.  
  448. int display_image_screen(file)
  449. char *file;
  450.  
  451. {
  452.    int done=0,retcode=0,cnt,colors,r,g,b;
  453.    struct IntuiMessage *msg;
  454.    struct Screen *screen;
  455.    struct Window *win;
  456.    struct ExecBase *SysBase=*(struct ExecBase **)4;
  457.  
  458.                                        /* now set window width & height */
  459.    tags[1].ti_Data=bmh.w;
  460.    tags[2].ti_Data=bmh.h+barheight;
  461.    tags[3].ti_Data=loadimg.bitmap1->Depth;
  462.    newscreen.Width = bmh.w;
  463.    newscreen.Height = bmh.h+barheight;
  464.    newscreen.Depth = loadimg.bitmap1->Depth;
  465.    newscreen.ViewModes = loadimg.modes;
  466.    if (!(screen=(struct Screen *)OpenScreen((struct NewScreen *)&newscreen)))
  467.       {
  468.       DisplayBeep(NULL);
  469.       return(1);                        /* if can't open screen, go back to WB window */
  470.       }
  471.    pic_win2.Width=bmh.w;
  472.    pic_win2.Height=bmh.h+barheight;
  473.    pic_win2.Screen = screen;
  474.    pic_win2.BitMap=loadimg.bitmap1;      /* ptr to refresh bitmap */
  475.    if (!(win=OpenWindow(&pic_win2)))
  476.       {
  477.       CloseScreen(screen);
  478.       DisplayBeep(NULL);
  479.       return(1);                        /* unable to open window */
  480.       }
  481.    colors=1<<loadimg.bitmap1->Depth;
  482.    if (IntuitionBase->LibNode.lib_Version >= 39)   /* if OS 3.0 or above */
  483.       {
  484.       for (cnt=0; cnt < colors; cnt++)
  485.          {
  486.          r=(color[cnt]&0x00ff0000)<<8;
  487.          g=(color[cnt]&0x0000ff00)<<16;
  488.          b=(color[cnt]&0x000000ff)<<24;
  489.          SetRGB32(&screen->ViewPort,cnt,r,g,b);
  490.          }
  491.       }
  492.    else
  493.       {
  494.       for (cnt=0; cnt < colors; cnt++)
  495.          {
  496.          r=(color[cnt]>>20)&0xf;
  497.          g=(color[cnt]>>12)&0xf;
  498.          b=(color[cnt]>>4)&0xf;
  499.          SetRGB4(&screen->ViewPort,cnt,r,g,b);
  500.          }
  501.       }
  502.    SetWindowTitles(win,screen_title,(UBYTE *)-1);
  503.    item1a_it.IText=wb_text;
  504.    SetMenuStrip(win,&menu1);            /* tack on the menu */
  505.    while (!done)
  506.       {
  507.       WaitPort(win->UserPort);         /* wait for Intuition message */
  508.       msg=(struct IntuiMessage *)GetMsg(win->UserPort);
  509.       switch (msg->Class)
  510.          {
  511.          case CLOSEWINDOW:
  512.             done=1;
  513.             break;
  514.          case MENUPICK:
  515.             switch (ITEMNUM(msg->Code))
  516.                {
  517.                case 0:
  518.                   retcode=1;
  519.                case 1:
  520.                   done=1;
  521.                   break;
  522.                default:
  523.                   break;
  524.                }
  525.             break;
  526.          default:
  527.             break;
  528.          }
  529.       ReplyMsg((struct Message *)msg);   /* tell Intuition we're done with msg, thanx */
  530.       }
  531.    SetWindowTitles(win,(UBYTE *)-1,NULL);
  532.    ClearMenuStrip(win);                  /* remove menu strip */
  533.    CloseWindow(win);
  534.    CloseScreen(screen);
  535.    return(retcode);
  536. }
  537.